home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11808 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  90 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: BC4.5 won't compile it; GNU will [templates]
  5. Message-ID: <marnoldDoCrts.Dp2@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4ib0bk$16cg@usenetz1.news.prodigy.com>
  8. Date: Sat, 16 Mar 1996 09:03:28 GMT
  9. Sender: marnold@netcom2.netcom.com
  10.  
  11. EWTE97A@prodigy.com (Gregory Gibson) writes:
  12.  
  13. >//GNU compiles this and it runs.  BC 4.5 does not compile it.
  14. >//And simple errors like (a<>b) instead of (a!=b) are ignored? 
  15.  
  16. What do you mean?  I can't find these comparisons anywhere in the code you
  17. posted.
  18.  
  19. >//What is wrong with BC 4.5?
  20.  
  21. I think it is rather "what is wrong with GNU C++?" (read on).
  22.  
  23. >// I APPRECIATE THE TIME INVESTMENT YOU MAKE!!!
  24.  
  25. >// BC 4.5 does not seem to recognize the global 'pool'
  26.  
  27.  
  28. That's because you attempt use the global variable "pool" *before* it's 
  29. definition is finished.  I think Borland C++ is perfectly justified in 
  30. telling you about a global variable that it does not yet know about.
  31.  
  32. To start with, your declaration for pool looks like this...
  33.  
  34.    memman<film> pool;
  35.  
  36. Seems simple enough.
  37.  
  38. But, to fully know what type pool is, the compiler must instantiate the 
  39. template memman<film>.  memman<film>, in turn (using an internal typedef), 
  40. instantiates list<film>, whose code refers to pool!  And (for those who 
  41. didn't try compiling the code) it's the references to pool inside list<>'s
  42. code that cause the error.
  43.  
  44. Well, no wonder there's an error; this is a circular reference!  pool depends
  45. on memman, which depends on list, which depends on pool.  Seems to me that, 
  46. as far as Borland C++ is concerned, pool does not exist when it's parsing 
  47. the code for list, since it's doing so to find out just what pool is in the
  48. first place!  pool probably hasn't been entered into the compiler's internal 
  49. symbol table.  So, when it sees the name pool in list<>'s code, it of course 
  50. generates the error, "Undefined symbol pool...".
  51.  
  52. If GNU compiles this file, I think it is being very "generous" with how it 
  53. lets you use global variables in this way.  It seems it treats variables as
  54. defined when it hasn't finished parsing their definitions.  I wouldn't rely 
  55. on this generousity if you want your code to compile elsewhere.  I think 
  56. Borland C++ is giving the correct result and your code indeed has an error.
  57.  
  58. The problem with your code is conceptually similar to this...
  59.  
  60.    template <class T>
  61.    class A
  62.       {
  63.       public:
  64.          void SomeFunc()
  65.             {
  66.             global_a.SomeFunc();   // error!
  67.             }
  68.       };
  69.  
  70.    A<int> global_a;
  71.  
  72.  
  73. Which generates the error, "Undefined symbol global_a..." while trying to 
  74. instantiate the variable "global_a".  Again, this is due to the circular 
  75. reference to global_a inside the code for A.  global_a depends on A<>, which 
  76. depends on global_a.  It simply does not compute.
  77.  
  78.  
  79. Have you tried you code on other compilers?
  80.  
  81.  
  82. Regards,
  83. -------------------------------------------------------------------------
  84. Matt Arnold                       |        | ||| | |||| |  | | || ||
  85. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  86. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  87. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  88. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  89. -------------------------------------------------------------------------
  90.